package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private  PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        // 获取房间id
        Long roomId = roomSubmitVo.getId();
        // 调用 mybatis-plus 自带的保存或更新方法
        saveOrUpdate(roomSubmitVo);

        // 判断是添加还是更新
        if (roomId != null) {
            // 不为null，更新，删除中间表的数据
            // 1. 根据房间id 和图片类型删除，图片列表
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,roomId));
            // 2. 根据房间id删除，属性信息列表
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomId));
            // 3. 根据房间id删除，配套信息列表
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomId));
            // 4. 根据房间id删除，标签信息列表
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,roomId));
            // 5. 根据房间id删除，支付方式列表
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomId));
            // 6. 根据房间id删除，可选租期列表
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomId));
        }

        // 向中间表中，插入数据
        // 1. 保存房间的 图片
        // 获取房间图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        // 验空
        if (!CollectionUtils.isEmpty(graphVoList)) {
            // 创建集合
            List<GraphInfo> graphInfoList = new ArrayList<>();
            // 遍历
            for (GraphVo graphVo : graphVoList) {
                // 设置 Graph 对象
                GraphInfo graphInfo = new GraphInfo();
                // 设置图片的名字
                graphInfo.setName(graphVo.getName());
                // 设置图片的url
                graphInfo.setUrl(graphVo.getUrl());
                // 设置房间的id
                graphInfo.setItemId(roomSubmitVo.getId());
                // 设置图片的类型
                graphInfo.setItemType(ItemType.ROOM);
                // 添加到集合
                graphInfoList.add(graphInfo);
            }
            // 批量保存
            graphInfoService.saveBatch(graphInfoList);
        }

        // 2. 保存房间的 属性信息
        // 获取房间属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        // 验空
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            // 创建集合
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            // 遍历
            for (Long attrValueId : attrValueIds) {
                // 创建 AttrValue 对象
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                // 添加到集合
                roomAttrValueList.add(roomAttrValue);
            }
            // 批量保存
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        // 3. 保存房间的 配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        // 4. 保存房间的 标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        // 5. 保存房间的 支付方式
        // 获取房间的id
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        // 验空
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            // 创建集合
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            // 遍历
            for (Long paymentTypeId : paymentTypeIds) {
                // 创建 PaymentType 对象
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                // 添加到集合
                roomPaymentTypeList.add(roomPaymentType);
            }
            // 批量保存
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        // 6. 保存房间的 可选租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    @Override
    public void deleteRoomInfoById(Long roomId) {
        // 根据房间id查询租约表看当前房间是否被入住
        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, roomId).in(LeaseAgreement::getStatus, 2, 5, 7));
        if (count > 0){
            // 房间被入住，不能删除
            throw new LeaseException(ResultCodeEnum.HAS_PERSON);
        }

        // 调用 Mybatis-plus 自带的方法删除公寓信息
        removeById(roomId);

        // 删除中间表的数据
        // 1. 根据房间id 和图片类型删除，图片列表
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,roomId));
        // 2. 根据房间id删除，属性信息列表
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomId));
        // 3. 根据房间id删除，配套信息列表
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomId));
        // 4. 根据房间id删除，标签信息列表
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,roomId));
        // 5. 根据房间id删除，支付方式列表
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomId));
        // 6. 根据房间id删除，可选租期列表
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomId));
    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        // 调用 Mybatis-Plus 中自带的方法查询房间信息
        RoomInfo roomInfo = getById(id);
        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        // 查询中间表的数据
        // 1. 查询房间的图片
        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoByRoomIdAndItemType(id,ItemType.ROOM);
        // 2. 查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoListByRoomId(id);
        // 3. 查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoListByRoomId(id);
        // 4. 查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoListByRoomId(id);
        // 5. 查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeListByRoomId(id);
        // 6. 查询可选租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermListByRoomId(id);

        // 创建 RoomDetailVo 对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 复制房间的属性值
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        // 设置公寓信息
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> getPageList(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return baseMapper.getPageList(roomItemVoPage,queryVo);
    }
}
